﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Koala.Pro.Common.Extension
{
    public static class DateExtensions
    {
        /// <summary>
        /// 获取指定日期所在旬的第一天
        /// </summary>
        /// <param name="date">日期</param>
        /// <returns>旬的第一天</returns>
        public static DateTime GetFirstDayOfDecade(this DateTime date)
        {
            int day = date.Day;
            int decadeStart = ((day - 1) / 10) * 10 + 1;
            return new DateTime(date.Year, date.Month, decadeStart);
        }

        /// <summary>
        /// 获取指定日期所在旬的最后一天
        /// </summary>
        /// <param name="date">日期</param>
        /// <returns>旬的最后一天</returns>
        public static DateTime GetLastDayOfDecade(this DateTime date)
        {
            int day = date.Day;
            int decadeStart = ((day - 1) / 10) * 10 + 1;
            return new DateTime(date.Year, date.Month, decadeStart + 9);
        }

        /// <summary>
        /// 获取指定日期所在季度的第一天
        /// </summary>
        /// <param name="date">日期</param>
        /// <returns>季度的第一天</returns>
        public static DateTime GetFirstDayOfQuarter(this DateTime date)
        {
            int month = date.Month - (date.Month - 1) % 3;
            return new DateTime(date.Year, month, 1);
        }

        /// <summary>
        /// 获取指定日期所在季度的最后一天
        /// </summary>
        /// <param name="date">日期</param>
        /// <returns>季度的最后一天</returns>
        public static DateTime GetLastDayOfQuarter(this DateTime date)
        {
            int month = date.Month - (date.Month - 1) % 3;
            return new DateTime(date.Year, month + 2, DateTime.DaysInMonth(date.Year, month + 2));
        }

        /// <summary>
        /// 获取指定日期所在年的第一天
        /// </summary>
        /// <param name="date">日期</param>
        /// <returns>年的第一天</returns>
        public static DateTime GetFirstDayOfYear(this DateTime date)
        {
            return new DateTime(date.Year, 1, 1);
        }

        /// <summary>
        /// 获取指定日期所在年的最后一天
        /// </summary>
        /// <param name="date">日期</param>
        /// <returns>年的最后一天</returns>
        public static DateTime GetLastDayOfYear(this DateTime date)
        {
            return new DateTime(date.Year, 12, 31);
        }

        /// <summary>
        /// 获取指定日期所在月份的天数
        /// </summary>
        /// <param name="date">日期</param>
        /// <returns>月份的天数</returns>
        public static int GetMonthDays(this DateTime date)
        {
            return DateTime.DaysInMonth(date.Year, date.Month);
        }

        /// <summary>
        /// 获取指定日期在一年中的第几天
        /// </summary>
        /// <param name="date">日期</param>
        /// <returns>一年中的第几天</returns>
        public static int DayInYear(this DateTime date)
        {
            return date.DayOfYear;
        }

        /// <summary>
        /// 计算两个日期之间的天数差
        /// </summary>
        /// <param name="date">起始日期</param>
        /// <param name="otherDate">结束日期</param>
        /// <returns>天数差</returns>
        public static int DaysBetween(this DateTime date, DateTime otherDate)
        {
            return Math.Abs((otherDate - date).Days);
        }

        /// <summary>
        /// 获取指定日期是星期几
        /// </summary>
        /// <param name="date">日期</param>
        /// <returns>星期几</returns>
        public static string GetDayOfWeek(this DateTime date)
        {
            return date.ToString("dddd");
        }

       
    }
}
